home *** CD-ROM | disk | FTP | other *** search
/ Wayzata's Best of Shareware PC/Windows 2 / Wayzata's Best of Shareware 2.0 (Windows) (Wayzata Technology)(7112)(1994).bin / pc / dos / telecomm / slrn_200 / slearn.slt < prev    next >
Text File  |  1992-09-20  |  12KB  |  419 lines

  1. //
  2. //  Module:       slearn.slt
  3. //                Part of SLEARN  (SALT learn utility)  v2.00
  4. //                Copyright (C) 1989-1992 Paul Roub.  All rights reserved.
  5. //  Version:      2.00
  6. //  Description:  SALT script to invoke SLEARN.EXE from Telix.
  7. //
  8. //                Recommended usage:  using TFE, set up a dialing directory
  9. //                entry for the BBS you wish to learn a script for.  Include
  10. //                'slearn' as the Script field for that system.
  11. //
  12. //  NOTES:        This script assumes that CS.EXE and SLEARN.EXE are both
  13. //                available from the script directory.  This means they must
  14. //                either be IN the script directory itself, or on the DOS
  15. //                PATH.
  16. //
  17. //                Whenver possible, this script uses the run() function to
  18. //                run other programs , as it is faster and uses less memory.
  19. //                However, we do occasionally have to call some DOS commands,
  20. //                so dos() is used there.  This is a bit inconsistent, but it
  21. //                is important, especially when calling SLEARN.EXE.  If that
  22. //                takes too long, we could lose characters, thus rendering
  23. //                SLEARN all but useless.
  24. //
  25. //  Author:       Paul Roub
  26. //
  27. //<f>
  28.  
  29.  
  30. //  various strings used by this script
  31. //
  32. str       CmdLine [128];              //  SLEARN command line
  33. str       fn      [ 12];              //  name of script file to create
  34. str       Deffn   [ 12];              //  'best guess' script file name
  35.  
  36. str       tmpfn   [] = "slearn.$$$";  //  name of temporary SALT file
  37.  
  38.                                       //  comm parameters:
  39. str       BaudStr [  6];              //    baud rate
  40. str       SetStr  [  3];              //    settings (e.g. E71)
  41.  
  42.  
  43. //  Change these if you hate the colors this script asks questions in.
  44. //
  45. int       AnswerColor   = 112;        //  default: black on grey
  46. int       BoxColor      = 7;          //  default: grey on black
  47. int       QuestionColor = 15;         //  default: white on black
  48.  
  49.  
  50. //  Set UseSetScr = 0 if you don't want SLEARN to add the new script's name
  51. //  to TELIX.FON, or if you use more than one dialing directory.
  52. //  Remember that even if UseSetScr == 1, SLEARN will still ask your
  53. //  permission before adding the script to TELIX.FON.
  54. //
  55. int       UseSetScr    = 1;           // default: add new script to TELIX.FON
  56.  
  57.  
  58. //<f>
  59. main()
  60. {
  61.   int       result;
  62.   int       SaveFile;
  63.   str       numstr[5];
  64.   str       dummy[81];
  65.   int       fonfile;
  66.   int       EntryPos;
  67.   str       entname[25];
  68.  
  69.   itos(get_baud(), BaudStr);          // get comm. parameters  (baud)
  70.  
  71.   if      (get_parity() == 0)         //                       (parity)
  72.     SetStr = "N";
  73.   else if (get_parity() == 1)
  74.     SetStr = "E";
  75.   else // (get_parity() == 2)
  76.     SetStr = "O";
  77.  
  78.   if (get_datab() == 7)               //                       (data bits)
  79.     strcat(SetStr, "7");
  80.   else
  81.     strcat(SetStr, "8");
  82.  
  83.   if (get_stopb() == 1)               //                       (stop bits)
  84.     strcat(SetStr, "1");
  85.   else
  86.     strcat(SetStr, "2");
  87.  
  88.   if (_script_dir)                    // change to script dir, if any
  89.     newdir(_script_dir);
  90.   else
  91.     newdir(_telix_dir);
  92.  
  93.   if (! filefind("slearn.cfg", 0, dummy))
  94.   {
  95.     call("slconfig");
  96.     if (_script_dir != "")            // change to script dir, if any
  97.       newdir(_script_dir);
  98.     else
  99.       newdir(_telix_dir);
  100.   }
  101.  
  102.                                       // build the command line:
  103.   CmdLine = "-b";                     //   baud
  104.   strcat(CmdLine, BaudStr );
  105.   strcat(CmdLine, " -s"   );          //   settings
  106.   strcat(CmdLine, SetStr  );
  107.   strcat(CmdLine, " "     );
  108.  
  109.   if (_strip_high)
  110.     strcat(CmdLine, "-h ");
  111.  
  112.   if (_entry_name != "")
  113.   {
  114.     strcat(CmdLine, "-n ^"");         //   entry name, if any
  115.     strcat(CmdLine, _entry_name);
  116.     strcat(CmdLine, "^" ");
  117.   }
  118.  
  119.   if (_entry_pass != "")              // _entry_pass, perhaps
  120.   {
  121.     strcat(CmdLine, "-w ^"");
  122.     strcat(CmdLine, _entry_pass);
  123.     strcat(CmdLine, "^" ");
  124.   }
  125.  
  126.   strcat(CmdLine, tmpfn   );          //   and the file to create
  127.  
  128.   result = run("slearn.exe", CmdLine, 0); // and run slearn
  129.  
  130.   clear_scr();
  131.  
  132.   if (result < 0)                   // Couldn't run it
  133.   {
  134.     prints("");
  135.     prints("Error: Couldn't find or run SLEARN.EXE.  SLEARN.EXE must either be in the");
  136.     prints("       TELIX script directory, or in a directory specified in the PATH");
  137.     prints("       environment variable.");
  138.     prints("");
  139.  
  140.     return;
  141.   }
  142.  
  143.   SaveFile = YesOrNo("Save the new script file?", "y");
  144.  
  145.   if (SaveFile)
  146.   {
  147.     DefSLTname(Deffn);                // get default SALT file name
  148.  
  149.     //  see what the user wants to call it
  150.     //
  151.     if (ask("Save with what name?", Deffn, fn, 12) == -1)
  152.       fn = Deffn;
  153.  
  154.     if (strpos(fn, ".", 0) == -1)     // tack on SLT extension if necessary
  155.       strcat(fn, ".slt");
  156.  
  157.     prints("");
  158.  
  159.     CmdLine = "copy ";                // copy temp file to permanent file
  160.     strcat(CmdLine, tmpfn);
  161.     strcat(CmdLine, " "  );
  162.     strcat(CmdLine, fn   );
  163.     dos(CmdLine, 0);
  164.  
  165.     CmdLine = "del ";                 // delete temp file
  166.     strcat(CmdLine, tmpfn);
  167.     dos(CmdLine, 0);
  168.  
  169.     SaveFile = YesOrNo("Compile the new script file?", "y");
  170.  
  171.     if (SaveFile)
  172.     {
  173.       result = run("cs.exe", fn, 0);  // and compile the new file
  174.  
  175.       if (result == -1)               // Couldn't run it
  176.       {
  177.         prints("");
  178.         prints("Error: Couldn't find or run CS.EXE.  CS.EXE must either be in the");
  179.         prints("       TELIX script directory, or in a directory specified in the PATH");
  180.         prints("       environment variable.");
  181.         prints("");
  182.       }
  183.     }
  184.   }
  185.   else
  186.   {
  187.     CmdLine = "del ";                 // otherwise, just trash the temp
  188.     strcat(CmdLine, tmpfn);
  189.     dos(CmdLine, 0);
  190.     result = 0;
  191.   }
  192.  
  193.   if (_telix_dir != "")               // get back where we started
  194.     newdir(_telix_dir);
  195.  
  196.   if (SaveFile && UseSetScr && (result == 0))
  197.     if (YesOrNo("Install this script in TELIX.FON?", "y"))
  198.     {
  199.       if (! filefind("zzz.fon", 0, dummy))
  200.       {
  201.         dos("copy telix.fon zzz.fon", 0);
  202.         status_wind("Dummy FON file ZZZ.FON was created.", 30);
  203.       }
  204.  
  205.       if (! filefind("zzz.fon", 0, dummy))
  206.         status_wind("Dummy FON file ZZZ.FON does not exist!  Can't write to TELIX.FON", 30);
  207.       else
  208.       {
  209.         loadfon("zzz.fon");
  210.  
  211.         dummy = "Setting script for entry ";
  212.         itos(_entry_enum, numstr);
  213.         strcat(dummy, numstr);
  214.         strcat(dummy, " (");
  215.         strcat(dummy, _entry_name);
  216.         strcat(dummy, ") to ^"");
  217.         strcat(dummy, fn);
  218.         strcat(dummy, "^"");
  219.  
  220.         status_wind(dummy, 20);
  221.  
  222.         fonfile = fopen("telix.fon", "r+");
  223.         if (fonfile <= 0)
  224.           status_wind("Error opening TELIX.FON", 20);
  225.         else
  226.         {
  227.           EntryPos = 64 + (86 * (_entry_enum - 1));
  228.           fseek(fonfile, EntryPos);
  229.           fread(entname, 25, fonfile);
  230.           setchr(entname, 25, 0);
  231.  
  232.           if (entname != _entry_name)
  233.             status_wind("Entry name does not match _entry_name.  Can't write to TELIX.FON");
  234.           else
  235.           {
  236.             setchr(fn, strpos(fn, ".", 0), 0);
  237.             fseek (fonfile, EntryPos + 46);
  238.             fwrite(fn, 12, fonfile);
  239.           }
  240.  
  241.           fclose(fonfile);
  242.         }
  243.  
  244.         loadfon("telix.fon");
  245.       }
  246.     }
  247.  
  248.   status_wind("Thank you for using SLEARN -- support shareware!", 15);
  249.  
  250.   return;
  251. }
  252.  
  253.  
  254. //<f>
  255. //
  256. //  Function:     DefSLTname
  257. //  Description:  Try to guess a default SALT script name from the Telix
  258. //                script directory and entry name.  We use the first 8
  259. //                alphanumeric characters in _entry_name, plus .SLT.  If
  260. //                _entry_name is blank (or devoid of alphanumerics), then we
  261. //                use the name 'new.slt' instead.
  262. //  Parameters:   str name - default name (returned)
  263. //  Returns:      nothing
  264. //
  265. DefSLTname(str name)
  266. {
  267.   int     DstCount, SrcCount;         // indices of source and dest strings
  268.  
  269.   DstCount = 0;
  270.   SrcCount = 0;
  271.  
  272.   while ((DstCount < 8) && (SrcCount < 25))
  273.   {
  274.     if (isalnum(subchr(_entry_name, SrcCount)))
  275.     {
  276.       setchr(name, DstCount, subchr(_entry_name, SrcCount));
  277.       DstCount = DstCount + 1;
  278.     }
  279.  
  280.     SrcCount = SrcCount + 1;
  281.   }
  282.  
  283.   setchr(name, DstCount, 0);
  284.  
  285.   if (strlen(name) == 0)
  286.     name = "new.slt";
  287.   else
  288.     strcat(name, ".slt");
  289.  
  290.   strlower(name);
  291.  
  292.   return;
  293. }
  294.  
  295.  
  296. //<f>
  297. //
  298. //  Function:     ask
  299. //  Description:  prompt the user for a string
  300. //  Parameters:   str question - prompt
  301. //                str default  - default value for answer
  302. //                str answer   - answer (returned)
  303. //                int max      - maximum length of answer
  304. //  Returns:      -1 if ESC hit
  305. //                length of answer otherwise
  306. //
  307. ask(str question, str default, str answer, int max)
  308. {
  309.   int       left, right;              // left and right sides of box
  310.   int       len;                      // length of longest string to display
  311.   int       result;                   // return code
  312.   int       SaveHandle;               // handle of saved screen area
  313.   int       top, bottom;              // top and bottom sides of box
  314.   int       x, y;                     // used to save/restore cursor position
  315.  
  316.   top    = 9;
  317.   bottom = 9 + 6;
  318.  
  319.   len = strlen(question);
  320.   if (len < strlen(default) + 9)
  321.     len = strlen(default) + 9;
  322.   if (len < max)
  323.     len = max;
  324.  
  325.   left = (80 - (len + 4)) / 2;
  326.   right = left + len + 3;
  327.  
  328.   x = getx();
  329.   y = gety();
  330.   SaveHandle = vsavearea(left, top, right, bottom);
  331.   box(left, top, right, bottom, 3, 0, BoxColor);
  332.  
  333.   pstraxy(question,    left + 2,  top + 2, QuestionColor);
  334.   pstraxy("Default: ", left + 2,  top + 3, BoxColor     );
  335.   pstraxy(default,     left + 11, top + 3, BoxColor     );
  336.  
  337.   //  Draw an 'input field' so the user can see right off the bat how long
  338.   //  'answer' is allowed to be.
  339.   //
  340.   pstraxy("            ", left + 2, bottom - 2, AnswerColor );
  341.  
  342.   result = getsxy(answer, max, left + 2, bottom - 2, AnswerColor);
  343.  
  344.   vrstrarea(SaveHandle);
  345.   gotoXY(x, y);
  346.  
  347.   if (result == 0)
  348.   {
  349.     answer = default;
  350.     result = strlen(answer);
  351.   }
  352.  
  353.   return(result);
  354. }
  355.  
  356.  
  357. //<f>
  358. //
  359. //  Function:     YesOrNo
  360. //  Description:  asks yes or no questions, with default
  361. //                displays question and default, and accepts one character
  362. //                if this character is CR or ESC, then default[0] is used
  363. //                keeps receiving characters until char is 'y' or 'n'
  364. //  Parameters:   str question - question to ask
  365. //                str default  - default string - "y" or "n"
  366. //  Returns:      1 if 'y' was entered
  367. //                2 if 'n' was entered
  368. //
  369. YesOrNo(str question, str default)
  370. {
  371.   int       ch;                       // user's input character
  372.   int       left, right;              // left and right sides of box
  373.   int       len;                      // length of question
  374.   int       SaveArea;                 // handle of saved screen area
  375.   int       top, bottom;              // top and bottom sides of box
  376.   int       x, y;                     // used to save/restore cursor position
  377.  
  378.   if (strlen(default) > 1)
  379.     setchr(default, 1, 0);
  380.  
  381.   strlower(default);
  382.   if ((default != "y") && (default != "n"))
  383.     default = " ";
  384.  
  385.   len = strlen(question);
  386.  
  387.   top = 10;
  388.   bottom = top + 4;
  389.   left = (80 - (len + 6)) / 2;
  390.   right = left + len + 5;
  391.  
  392.   x = getx();
  393.   y = gety();
  394.  
  395.   SaveArea = vsavearea(left, top, right, bottom);
  396.   box(left, top, right, bottom, 3, 0, BoxColor);
  397.  
  398.   pstraxy(question, left + 2,           top + 2, QuestionColor);
  399.   pstraxy(default,  left + 2 + len + 1, top + 2, AnswerColor);
  400.   gotoxy (left + 2 + len + 1, top + 2);
  401.  
  402.   do
  403.   {
  404.     ch = inkeyw();
  405.  
  406.     if ((ch == 13) || (ch == 27))
  407.       ch = subchr(default, 0);
  408.  
  409.     ch = tolower(ch);
  410.   } while ((ch != 'y') && (ch != 'n'));
  411.  
  412.   vrstrarea(SaveArea);
  413.  
  414.   gotoxy(x, y);
  415.  
  416.   return(ch == 'y');                  // true if 'y', false if 'n'
  417. }
  418.  
  419.